package com.xc.disk.web.rest;

import com.sun.mail.imap.protocol.ID;
import com.xc.disk.bean.*;
import com.xc.disk.config.Constants;
import com.xc.disk.dto.*;
import com.xc.disk.exception.OperateException;
import com.xc.disk.info.TokenInfo;
import com.xc.disk.entity.UserEntity;
import com.xc.disk.service.AuthorizeService;
import com.xc.disk.service.MailService;
import com.xc.disk.service.UserService;
import com.xc.disk.utils.RedisUtils;
import com.xc.tool.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.TreeMap;

/**
 * <p>项目名称: Stars</p>
 * <p>描述: 授权接口</p>
 * <P>创建人:小程 </p>
 * <P>创建人邮箱: 779943476@qq.com</p>
 * <p>创建时间: 2019/10/24</p>
 *
 * @version v1.0
 */
@RestController
public class AuthorizeRest {
    @Autowired
    private AuthorizeService authorizeService;
    @Autowired
    private UserService userService;
    @Autowired
    private MailService mailService;
    @Autowired
    private Constants constants;

    /**
     * 登录，获取token
     *
     * @param loginBean 登录参数
     * @return 返回token信息
     */
    @PostMapping("/login")
    public LoginDto login(@RequestBody LoginBean loginBean, HttpServletRequest request) {
        return authorizeService.login(loginBean, IpUtils.getClientIp(request));
    }

    /**
     * 更新token信息
     *
     * @param token token
     * @return 返回token信息
     */
    @PutMapping("/token/update")
    public TokenInfo updateToken(String token, String clientIp) {
        return authorizeService.updateToken(token, clientIp);
    }

    /**
     * 删除token信息
     *
     * @param token token信息
     */
    @DeleteMapping("/token/delete")
    public void deleteToken(TokenInfo token) {
        authorizeService.deleteToken(token.getToken());
    }


    /**
     * 注册
     *
     * @param registerBean 注册参数信息
     * @return 登录成功信息
     */
    @PostMapping("/register")
    public LoginDto register(@RequestBody RegisterBean registerBean, HttpServletRequest request) {
        return authorizeService.register(registerBean, IpUtils.getClientIp(request));
    }

    /**
     * 找回密码
     *
     * @param forgetBean 找回密码需要参数
     */
    @PostMapping("/forget")
    public void forgetPassword(@RequestBody ForgetBean forgetBean) {
        authorizeService.forgetPassword(forgetBean);
    }

    /**
     * 修改密码
     *
     * @param token        用户token
     * @param passwordBean 新旧密码
     */
    @PutMapping("/password")
    public UserDto updatePassword(TokenInfo token, @RequestBody PasswordBean passwordBean) {
        UserEntity userEntity = userService.getUserById(token.getUserId());
        if (!Md5Utils.verifySaltMd5(passwordBean.getPassword(), userEntity.getPassword())) {
            throw new OperateException("f036", "修改失败,旧密码不正确!");
        }
        userEntity.setPassword(Md5Utils.getSaltMd5(passwordBean.getNewPassword()));
        return ObjectUtils.convert(new UserDto(), userService.updateUser(userEntity));
    }

    /**
     * 发送邮箱验证码
     *
     * @param mailVerifyCode 邮箱地址
     */
    @PostMapping("/send/mail/verify/code")
    public void sendMailVerifyCode(@RequestBody MailVerifyCode mailVerifyCode) {
        String code = StringUtils.random(6);
        String html = "<div style=\"width:100%;text-align: center\">\n" +
                "    <p style=\"margin:0;padding:0;line-height:30px;font-size:14px;color:#333;font-family:'宋体',arial,sans-serif\">\n" +
                "        您好！感谢您使用本系统，本次请求的验证码为：\n" +
                "    </p>\n" +
                "    <p style=\"margin:0;padding:0;line-height:30px;font-size:14px;color:#333;font-family:'宋体',arial,sans-serif\">\n" +
                "        <b style=\"font-size:18px;color:#f90\">" + code + "</b>\n" +
                "        <p style=\"\n" +
                "        margin:0px 0px 0px 10px;\n" +
                "        padding:0;\n" +
                "        line-height:30px;\n" +
                "        font-size:14px;\n" +
                "        color:#979797;\n" +
                "        font-family:'宋体',arial,sans-serif\">(为了保障您帐号的安全性，请在30分钟内完成验证。)</p>\n" +
                "    </p>\n" +
                "</div>";
        MailHtmlBean mailHtmlBean = new MailHtmlBean(mailVerifyCode.getMailbox());
        mailHtmlBean.setHtml(html);
        if (mailVerifyCode.getType().equals("register")) {
            String key = mailVerifyCode.getType() + mailVerifyCode.getMailbox();
            RedisUtils.set(key, code, 1800000l);
            mailHtmlBean.setTitle("注册");
        } else if (mailVerifyCode.getType().equals("forget")) {
            String key = mailVerifyCode.getType() + mailVerifyCode.getMailbox();
            RedisUtils.set(key, code, 1800000l);
            mailHtmlBean.setTitle("找回密码");
        }
        mailService.sendHtmlMail(mailHtmlBean);
    }

    /**
     * 验证账号
     *
     * @param account
     * @return
     */
    @GetMapping("/verify/account")
    public VerifyAccountDto verifyAccount(String account) {
        return authorizeService.verifyAccount(account);
    }
}
